package com.yijianonghu.controller.wxwebsite.sannong;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yijianonghu.service.wxwebsite.sannongdongtai.SanNongDongTaiDetailService;
import com.yijianonghu.service.wxwebsite.sannongdongtai.SanNongDongTaiListService;
import com.yijianonghu.util.Constants;
import com.yijianonghu.util.exception.NoResultReturnedException;
import com.yijianonghu.util.exceptionhandler.ControllerExceptionHandler;
import com.yijianonghu.util.result.MessageCode;
import com.yijianonghu.util.result.Result;

@Controller
@RequestMapping(value=Constants.WXConstants.WX_PREFIX+"sannongdongtai/", method=RequestMethod.POST)
public class SanNongDongTaiController {
	
	private static final Logger logger=LoggerFactory.getLogger(SanNongDongTaiController.class);
	
	@Autowired
	private SanNongDongTaiDetailService sanNongDongTaiDetailService;
	
	@Autowired
	private SanNongDongTaiListService sanNongDongTaiListService;
	
	@Autowired
	private ControllerExceptionHandler exceptionHandler;
	
	@RequestMapping(value="getTitles")
	public @ResponseBody Result getTitles(){
		Result result=new Result();
		result.setSuccess(false);
		List<Map<String, Object>> list=null;
		try {
			list = sanNongDongTaiListService.getSanNongDongTaiList();
			if(list==null || list.isEmpty()){
				throw new NoResultReturnedException("三农动态列表为空！");
			}
		} catch (NoResultReturnedException e) {
			exceptionHandler.handleException(logger, "三农动态列表为空！", result, MessageCode.WX_NO_RESULT_RETURNED, e);
		} catch(Exception e){
			exceptionHandler.handleException(logger, "三农动态列表获取失败！", result, MessageCode.SYSTEM_BUSY, e);
		}
		if(list!=null && (!list.isEmpty())){
			result.setInfor(list);
			result.setSuccess(true);
		}
		return result;
	}
	
	@RequestMapping(value="getDetail")
	public @ResponseBody Result getDetail(Integer id){
		Result result=new Result();
		result.setSuccess(false);
		String detailContent=null;
		try {
			detailContent=sanNongDongTaiDetailService.getSanNongDongTaiDetail(id);
			if(detailContent==null || detailContent.isEmpty()){
				throw new NoResultReturnedException("三农动态详情为空！");
			}
		} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException e) {
			logger.error("获取三农动态详情失败！",e);
			exceptionHandler.handleException(logger, "获取三农动态详情失败！", result, MessageCode.SYSTEM_BUSY, e);
		} catch (NoResultReturnedException e) {
			exceptionHandler.handleException(logger, "三农动态详情为空！", result, MessageCode.WX_NO_RESULT_RETURNED, e);
		}
		if(!StringUtils.isEmpty(detailContent)){
			result.setInfor(detailContent);
			result.setSuccess(true);
		}
		return result;
	}
	
}
